package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    /**
     * 分页查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(SensitiveDto dto) {

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> lam = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getName())){
            lam.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage pageList = page(page, lam);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)pageList.getTotal());

        pageResponseResult.setData(pageList.getRecords());

        return pageResponseResult;
    }

    /**
     * 新增
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult add(AdSensitive adSensitive) {

        //StringUtils.isEmpty  如果内容为空  是true
        if (adSensitive == null || StringUtils.isEmpty(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }


        adSensitive.setCreatedTime(new Date());

        save(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //StringUtils.isEmpty  如果内容为空  是true
        if (adSensitive == null || StringUtils.isEmpty(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive ad = getById(id);
        if (ad == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
